﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.BusinessLayer;

namespace BSCMFF.Web.Pages.Evaluation_Setup
{
    public partial class EvaluationBonusCalculation : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (EvalSelection.PublicSelectedValue != 0)
            {
                BSCEntities context = new BSCEntities();

                Evaluation_Period ep = (from eps in context.Evaluation_Period
                                        where eps.ID == EvalSelection.PublicSelectedValue
                                        select eps).SingleOrDefault();

                if (ep.Reward == true)
                {
                    ShowCalculation(ep);
                    LabelNoReward.Visible = false;
                }
                else
                {
                    LabelNoReward.Visible = true;
                }
            }
        }

        private void ShowCalculation(Evaluation_Period ep)
        {
            if (ep.PKPIs == true)
            {
                PanelPKPIsBonus.Visible = true;
                ShowPKPIsCalculation(ep);
            }
        }

        private void ShowPKPIsCalculation(Evaluation_Period ep)
        {
            decimal totalBonus = (decimal)ep.TotalBonus;

            decimal pkpisBonus = ep.Competencies == false ? totalBonus : totalBonus * (decimal)ep.BonusSliderValue / 100;

            RadNumericTextBoxCurrentBonusForPKPIs.Value = (double)pkpisBonus;

        }

        protected void RadButtonCalculate_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();

            Evaluation_Period ep = (from eps in context.Evaluation_Period
                                    where eps.ID == EvalSelection.PublicSelectedValue
                                    select eps).SingleOrDefault();

            double perc = SigmoidHelper.Sigmoid((int)RadNumericTextBoxCurrentScore.Value, (double)ep.SigmoidA, (double)ep.SigmoidB);

            List<int> userIDs = (from eee in context.Evaluation_Evaluator_Employee
                                where eee.Evaluation_FK == ep.ID
                                select eee.Employee_FK).Distinct().ToList();
            int numberOfEvaluatedEmployees = 0;
            if (userIDs != null)
            {
                numberOfEvaluatedEmployees = userIDs.Count;
            }

            if (numberOfEvaluatedEmployees != 0)
            {
                double result = (double)RadNumericTextBoxCurrentBonusForPKPIs.Value * perc / numberOfEvaluatedEmployees / 100;

                RadNumericTextBoxCalculated.Value = result;
            }
        }
    }
}